/*
 * @版权所有(C): 1999-2021,西安交大捷普网络科技有限公司
 * @FilePath: \src\baselib\LibJumpJson\JumpXml.cpp
 * @作者: 岳保保
 * @Date: 2023-01-05 13:40:39
 * @功能描述:
 */
#include "JumpXml.h"
#include <sstream>
JumpXml::JumpXml()
    :m_xmlelement(NULL),
    m_xmldocument(NULL)
{
}

JumpXml::JumpXml(const JumpXml& obj)
{
}

JumpXml& JumpXml::operator=(const JumpXml& obj)
{
    // 避免自赋值
    if (this == &obj)
    {
        return *this;
    }
    return *this;
}

void JumpXml::setXmlRootName(const std::string& rootName)
{
    //设置xml root名称
    m_rootName = rootName;
}

bool JumpXml::setXml(const std::string& xmlStr)
{
    m_xmldocument = new tinyxml2::XMLDocument;
    // 设置xml str 赋值给对象对应的值
    if (tinyxml2::XMLError::XML_SUCCESS != m_xmldocument->Parse(xmlStr.c_str()))
    {
        m_xmldocument->Clear();
        delete m_xmldocument;
        m_xmldocument = nullptr;
        return false;
    }
    m_xmlelement = m_xmldocument->RootElement();
    if (!m_xmlelement)
    {
        m_xmldocument->Clear();
        delete m_xmldocument;
        m_xmldocument = nullptr;
        return false;
    }
    xml2Val();
    m_xmldocument->Clear();
    delete m_xmldocument;
    m_xmldocument = nullptr;
    return true;
}


/**
 * @功能描述: 从文件中获取xml
 * @param {string} &xmlPath xml 路径
 * @返回值: 成功或者失败
 */
bool JumpXml::loadXmlFromFile(const std::string& xmlPath)
{
    m_xmldocument = new tinyxml2::XMLDocument;
    if (tinyxml2::XMLError::XML_SUCCESS != m_xmldocument->LoadFile(xmlPath.c_str()))
    {
        m_xmldocument->Clear();
        delete m_xmldocument;
        m_xmldocument = nullptr;
        return false;
    }
    m_xmlelement = m_xmldocument->RootElement();
    if (!m_xmlelement)
    {
        m_xmldocument->Clear();
        delete m_xmldocument;
        m_xmldocument = nullptr;
        return false;
    }
    xml2Val();
    m_xmldocument->Clear();
    delete m_xmldocument;
    m_xmldocument = nullptr;
    return true;
}

const std::string JumpXml::getXml(bool needHeader, const std::string& version, const std::string& encoding)
{
    tinyxml2::XMLPrinter* xmlItemPrinter = new XmlItem;
    //不存在父级标签
    m_xmldocument = new tinyxml2::XMLDocument;

    m_xmlelement = m_xmldocument->NewElement(m_rootName.c_str());
    m_xmldocument->InsertEndChild(m_xmlelement);
    if (needHeader)
    {
        dynamic_cast<XmlItem*>(xmlItemPrinter)->pushXmlHeader(version, encoding);
    }

    // 将对象转为xml str
    val2Xml();
    m_xmldocument->Print(xmlItemPrinter);
    std::string strResult = std::string(xmlItemPrinter->CStr());
    delete xmlItemPrinter;
    xmlItemPrinter = nullptr;

    m_xmldocument->Clear();
    delete m_xmldocument;
    m_xmldocument = nullptr;
    return strResult;
}

void JumpXml::setXMLDocument(tinyxml2::XMLDocument* preDocument)
{
    //获取上级节点
    m_xmldocument = preDocument;
}

void JumpXml::setXMLElement(tinyxml2::XMLElement* curentNode)
{
    //获取本级节点
    m_xmlelement = curentNode;
}

bool JumpXml::xml2Object(const std::string& memberName, JumpXml& objValue)
{
    //将xml字符串转为对象值
    tinyxml2::XMLElement* element = XmlItem::getXmlChildeEleByName(m_xmlelement, memberName);
    if (element)
    {
        objValue.setXMLDocument(m_xmldocument);
        objValue.setXMLElement(element);
        objValue.xml2Val();
        return true;
    }
    return false;
}

bool JumpXml::object2Xml(const std::string& memberName, JumpXml& objValue)
{
    //将对象值转为xml字符串
    tinyxml2::XMLElement* element = XmlItem::getXmlChildeEleByName(m_xmlelement, memberName);
    if (element)
    {
        // 将document传递给下级
        objValue.setXMLDocument(m_xmldocument);
        objValue.setXMLElement(element);
        objValue.val2Xml();
        m_xmlelement->InsertEndChild(element);
    }
    else
    {
        tinyxml2::XMLElement* element = m_xmldocument->NewElement(memberName.c_str());
        //是指子集的属性
        objValue.setXMLDocument(m_xmldocument);
        objValue.setXMLElement(element);
        objValue.val2Xml();
        m_xmlelement->InsertEndChild(element);
    }
    return true;
}
